package com.lubase.wfengine.node.operator;

import com.lubase.orm.exception.WarnCommonException;
import com.lubase.orm.extend.IColumnRemoteService;
import com.lubase.model.DbCollection;
import com.lubase.model.DbEntity;
import com.lubase.wfengine.auto.entity.WfFInsEntity;
import com.lubase.wfengine.auto.entity.WfOperEntity;
import com.lubase.wfengine.auto.entity.WfTaskEntity;
import com.lubase.wfengine.model.EOperatorType;
import com.lubase.wfengine.model.OperatorUserModel;
import com.lubase.wfengine.node.OperatorService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class FormFieldService implements OperatorService {

    @Autowired
    @Qualifier("userColumnServiceImpl")
    IColumnRemoteService remoteServiceById;

    @Autowired
    @Qualifier("userInfoByCodeServiceImpl")
    IColumnRemoteService remoteServiceByCode;

    @Override
    public EOperatorType getOperatorType() {
        return EOperatorType.FormField;
    }

    @SneakyThrows
    @Override
    public List<OperatorUserModel> getUserIdList(WfTaskEntity taskEntity, WfOperEntity operEntity, WfFInsEntity fIns, DbCollection collBisData) {
        List<OperatorUserModel> userList = new ArrayList<>();
        DbEntity bisData = collBisData.getData().get(0);
        String operator = operEntity.getOper_value();
        if (StringUtils.isEmpty(operator)) {
            throw new WarnCommonException("处理者设置错误，请联系管理员");
        }
        if (!bisData.containsKey(operator) || StringUtils.isEmpty(bisData.get(operator).toString())) {
            throw new WarnCommonException(String.format("在业务数据中未找到字段 %s 的值", operEntity.getOper_value()));
        }
        for (String userIdOrCode : bisData.get(operator).toString().split(",")) {
            OperatorUserModel userModel = new OperatorUserModel();
            //指定处理人支持工号，为了兼容以前的id 暂时保留两处判断逻辑
            DbEntity userEntity = null;
            if (userIdOrCode.matches("\\d+")) {
                userEntity = remoteServiceById.getCacheDataByKey(userIdOrCode);
                if (userEntity == null || userEntity.getId() == null) {
                    userEntity = remoteServiceByCode.getCacheDataByKey(operEntity.getOper_value());
                }
                userModel.setUserName(userEntity.get(remoteServiceById.displayCol()).toString());
            } else {
                userEntity = remoteServiceByCode.getCacheDataByKey(userIdOrCode);
                userModel.setUserName(userEntity.get(remoteServiceByCode.displayCol()).toString());
            }
            userModel.setUserId(userEntity.getId().toString());
            userList.add(userModel);
        }
        return userList;
    }
}
